package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.CatTypeMasterDao;
import com.pristine.domain.CategoryTypeMasterEntity;
import com.pristine.service.wsdl.CategoryTypeMasterVO;
import com.pristine.service.wsdl.ICatTypeMasterService;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("catTypeMasterService")
@Transactional
public class CatTypeMasterServiceImpl implements ICatTypeMasterService {

	@Autowired
	private CatTypeMasterDao catTypeMasterDao;

	@Override
	public Integer createCategoryType(CategoryTypeMasterVO categoryTypeVO) {
		CategoryTypeMasterEntity categoryType = VOToEntityConverter
				.convert(categoryTypeVO);
		catTypeMasterDao.add(categoryType);
		return categoryType.getCatTypeId();
	}

	@Override
	public Integer updateCategoryType(CategoryTypeMasterVO categoryTypeVO) {
		CategoryTypeMasterEntity categoryType = VOToEntityConverter
				.convert(categoryTypeVO);
		catTypeMasterDao.update(categoryType);
		return categoryType.getCatTypeId();
	}

	@Override
	public CategoryTypeMasterVO getCategoryTypeById(Integer id) {
		CategoryTypeMasterEntity categoryType = catTypeMasterDao.find(id);
		CategoryTypeMasterVO categoryTypeVO = EntityToVOConverter
				.convert(categoryType);
		return categoryTypeVO;
	}

	@Override
	public boolean deleteCategoryType(Integer id) {
		try {
			CategoryTypeMasterEntity categoryType = catTypeMasterDao.find(id);
			catTypeMasterDao.remove(categoryType);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<CategoryTypeMasterVO> getAllCategoryTypeMaster() {
		List<CategoryTypeMasterEntity> list = catTypeMasterDao.list();
		List<CategoryTypeMasterVO> voList=  new ArrayList<CategoryTypeMasterVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(CategoryTypeMasterEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}

}
